package com.qzdsoft.erpcloud.service.pu.impl;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import com.qzdsoft.erpcloud.domain.pu.PurchaseOrder;
import com.qzdsoft.erpcloud.mapper.pu.PurchaseOrderDetailMapper;
import com.qzdsoft.erpcloud.mapper.pu.PurchaseOrderMapper;
import com.qzdsoft.erpcloud.mapper.pu.PurchaseOrderQueryMapper;
import com.qzdsoft.erpcloud.service.pu.PurchaseOrderService;
import com.qzdsoft.erpcloud.vo.pu.PurchaseOrderEditInfo;
import com.qzdsoft.erpcloud.vo.pu.PurchaseOrderInfo;
import com.qzdsoft.erpcloud.vo.pu.PurchaseOrderQueryInfo;
import com.qzdsoft.utils.DateUtil;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;
import com.qzdsoft.vo.pu.PurchaseOrderStatus;

@Service
public class PurchaseOrderServiceImpl implements PurchaseOrderService {
	
    private static final Logger logger = LoggerFactory.getLogger(PurchaseOrderServiceImpl.class);
	
    @Autowired
	private PurchaseOrderQueryMapper purchaseOrderQueryMapper;
    
    @Autowired
    private PurchaseOrderDetailMapper purchaseOrderDetailMapper;
	
    @Autowired
    private PurchaseOrderMapper purchaseOrderMapper;
	@Override
	public LayTableResponse<PurchaseOrderInfo> findPurchaseOrdersBy(PurchaseOrderQueryInfo info) {
	    logger.debug("查询采购订单，参数：{}",info);
	    //指定时间+1天
	    if(!StringUtil.isEmpty(info.getEndDate())) {
	        info.setEndDate(DateUtil.getSpecifiedDayAfter(info.getEndDate()));
	    }
		PageHelper.startPage(info.getPage(), info.getLimit());
		List<PurchaseOrderInfo> purchaseOrders = purchaseOrderQueryMapper.selectAll(info);
		LayTableResponse<PurchaseOrderInfo> po = new LayTableResponse<PurchaseOrderInfo>(purchaseOrders);
		return po;
	}
	
	@Override
	public PurchaseOrderInfo findPurchaseOrderBy(Integer id) {
		return purchaseOrderQueryMapper.selectOneBy(id);
	}

	@Transactional
	@Override
	public Response<String> edit(PurchaseOrderEditInfo info) {
	    PurchaseOrder purchaseOrder = purchaseOrderMapper.selectByPrimaryKey(info.getId());
	    if(purchaseOrder==null) {
            logger.error("编辑采购订单，根据id:{}查询到的采购订单为空",info.getId());
            throw new RuntimeException("根据id查询到的采购订单为空") ;
        }
	    purchaseOrder.setAddr(info.getAddress());
        purchaseOrder.setCompanyId(info.getCompanyid());
        purchaseOrder.setSourceBillId(info.getSourceBillid());
        purchaseOrder.setSourceBillNo(info.getSourceBillno());
        purchaseOrder.setSupplyId(info.getSupplierId());
        purchaseOrder.setSupplyContactId(info.getConnactId());
        purchaseOrder.setStatementCurrencyId(info.getCurrencyId());
        purchaseOrder.setStatementDays(info.getSettleDays());
        purchaseOrder.setStatementExchangeRate(Integer.parseInt(info.getExchargeRate()));
        purchaseOrder.setStatementId(info.getSettleMethodId());
        purchaseOrder.setStatementTaxRate(Integer.parseInt(info.getTaxRate()));
        purchaseOrder.setStatementName(info.getSettleMethodName());
        purchaseOrder.setSourceType(info.getSourceType());
        purchaseOrder.setModifyTime(new Date());
        purchaseOrder.setModifyId(info.getUserid());
        purchaseOrderMapper.updateByPrimaryKey(purchaseOrder);
		return new Response<String>(ResultEnum.SUCCESS);
	}
	
	@Transactional
    @Override
    public Response<PurchaseOrderInfo> reivew(Integer userid,Integer id)
    {
        PurchaseOrder record = purchaseOrderMapper.selectByPrimaryKey(id);
        if(record==null) {
            logger.error("审核采购订单，根据id:{}查询到的采购订单为空",id);
            throw new RuntimeException("根据id查询到的采购订单为空") ;
        }
        record.setReviewId(userid);
        record.setReviewTime(new Date());
        record.setStatus(Integer.parseInt(PurchaseOrderStatus.HAS_REVIEW_CODE));
        int count = purchaseOrderMapper.updateByPrimaryKey(record);
        if(count==1) {
            PurchaseOrderInfo purchaseOrderInfo = purchaseOrderQueryMapper.selectOneBy(id);
            return new Response<PurchaseOrderInfo>(ResultEnum.SUCCESS,purchaseOrderInfo);
        }else {
            return new Response<PurchaseOrderInfo>(ResultEnum.ERROR);
        }
    }
    
	@Transactional
    @Override
    public Response<PurchaseOrderInfo> cancelReview(Integer userid,Integer id)
    {
        PurchaseOrder record = purchaseOrderMapper.selectByPrimaryKey(id);
        if(record==null) {
            logger.error("弃审采购订单，根据id:{}查询到的采购订单为空",id);
            throw new RuntimeException("根据id查询到的采购订单为空") ;
        }
        record.setModifyId(userid);
        record.setReviewTime(new Date());
        record.setStatus(Integer.parseInt(PurchaseOrderStatus.NOT_REVIEW_CODE));
        int count = purchaseOrderMapper.updateByPrimaryKey(record);
        if(count==1) {
            PurchaseOrderInfo purchaseOrderInfo = purchaseOrderQueryMapper.selectOneBy(id);
            return new Response<PurchaseOrderInfo>(ResultEnum.SUCCESS,purchaseOrderInfo);
        }else {
            return new Response<PurchaseOrderInfo>(ResultEnum.ERROR);
        }
    }
    
	@Transactional
    @Override
    public Response<String> close(Integer id)
    {
        PurchaseOrder record = purchaseOrderMapper.selectByPrimaryKey(id);
        if(record==null) {
            logger.error("关闭采购订单，根据id:{}查询到的采购订单为空",id);
            throw new RuntimeException("根据id查询到的采购订单为空") ;
        }
        record.setReviewTime(new Date());
        record.setStatus(Integer.parseInt(PurchaseOrderStatus.CLOSE_CODE));
        int count = purchaseOrderMapper.updateByPrimaryKey(record);
        if(count==1) {
            return new Response<String>(ResultEnum.SUCCESS);
        }else {
            return new Response<String>(ResultEnum.ERROR);
        }
    }

    @Transactional
    @Override
    public Response<String> remove(Integer id)
    {
        int orderDelCount = purchaseOrderMapper.deleteByPrimaryKey(id);
        if(orderDelCount!=1) {
            logger.error("关闭采购订单，根据id:{}查询到的采购订单为空",id);
            throw new RuntimeException("根据id查询到的采购订单为空") ;
        }
        int count = purchaseOrderDetailMapper.deleteByBillid(id);
        logger.debug("根据billid：{}删除采购订单明细行数：{}",id,count);
        return new Response<String>(ResultEnum.SUCCESS);
    }

    @Transactional
    @Override
    public Response<String> add(PurchaseOrderEditInfo info)
    {
        PurchaseOrder purchaseOrder = new PurchaseOrder();
        purchaseOrder.setAddr(info.getAddress());
        purchaseOrder.setBillNo(info.getBillno());
        purchaseOrder.setCompanyId(info.getCompanyid());
        purchaseOrder.setSourceBillId(info.getSourceBillid());
        purchaseOrder.setSourceBillNo(info.getSourceBillno());
        purchaseOrder.setSupplyId(info.getSupplierId());
        purchaseOrder.setSupplyContactId(info.getConnactId());
        purchaseOrder.setStatementCurrencyId(info.getCurrencyId());
        purchaseOrder.setStatementDays(info.getSettleDays());
        purchaseOrder.setStatementExchangeRate(Integer.parseInt(info.getExchargeRate()));
        purchaseOrder.setStatementId(info.getSettleMethodId());
        purchaseOrder.setStatementTaxRate(Integer.parseInt(info.getTaxRate()));
        purchaseOrder.setStatementName(info.getSettleMethodName());
        purchaseOrder.setSourceType(info.getSourceType());
        purchaseOrder.setCtime(new Date());
        purchaseOrder.setStatus(Integer.parseInt(PurchaseOrderStatus.NOT_REVIEW_CODE));
        int i = purchaseOrderMapper.insert(purchaseOrder);
        if(i>0) {
            return new Response<>(ResultEnum.SUCCESS);
        }else {
            return new Response<>(ResultEnum.ERROR);
        }
    }

	@Override
	public List<PurchaseOrder> selectReviewOrderByCompanyId(PurchaseOrder record) {
		return purchaseOrderMapper.select(record);
	}
    
    
}
